﻿using Pdoxcl2Sharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using V3MainTool.Model;

namespace V3MainTool.UI
{
    /// <summary>
    /// UCTech.xaml 的交互逻辑
    /// </summary>
    public partial class UCTech : UserControl
    {
        TechFamily productionTech = new TechFamily();
        TechFamily militaryTech = new TechFamily();
        TechFamily societyTech = new TechFamily();
        string fold;
        public UCTech()
        {
            InitializeComponent();
            cmbType.Items.Add("建造");
            cmbType.Items.Add("军事");
            cmbType.Items.Add("社会");
            cmbType.SelectedIndex = 0;
            cmbType.SelectionChanged += CmbType_SelectionChanged;
        }

        private void CmbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadSelectedList();
        }

        private void LoadSelectedList()
        {
            switch (cmbType.SelectedIndex)
            {
                case 0: dgTech.ItemsSource = productionTech.MyList; break;
                case 1: dgTech.ItemsSource = militaryTech.MyList; break;
                case 2: dgTech.ItemsSource = societyTech.MyList; break;
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (productionTech == null)
                {
                    MessageBox.Show("请先加载");
                    return;
                }
                using (FileStream fs = new FileStream(fold + @"\common\technology\technologies\10_production.new.txt", FileMode.OpenOrCreate))
                {
                    using (ParadoxSaver saver = new ParadoxSaver(fs))
                    {
                        productionTech.Write(saver);
                    }
                }
                using (FileStream fs = new FileStream(fold + @"\common\technology\technologies\20_military.new.txt", FileMode.OpenOrCreate))
                {
                    using (ParadoxSaver saver = new ParadoxSaver(fs))
                    {
                        militaryTech.Write(saver);
                    }
                }
                using (FileStream fs = new FileStream(fold + @"\common\technology\technologies\30_society.new.txt", FileMode.OpenOrCreate))
                {
                    using (ParadoxSaver saver = new ParadoxSaver(fs))
                    {
                        societyTech.Write(saver);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void btnReload_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                fold = MainWindow.Instance.GetFold();
                string localfile = fold + @"\localization\simp_chinese\inventions_l_simp_chinese.yml";
                string productionPath = fold + @"\common\technology\technologies\10_production.txt";
                string militaryPath = fold + @"\common\technology\technologies\20_military.txt";
                string societyPath = fold + @"\common\technology\technologies\30_society.txt";
                LocalHelper.InventionsDic = LocalHelper.GetDic(localfile);

                using (FileStream fs = new FileStream(productionPath, FileMode.Open))
                {
                    productionTech = ParadoxParser.Parse(fs, new TechFamily());
                }
                using (FileStream fs = new FileStream(militaryPath, FileMode.Open))
                {
                    militaryTech = ParadoxParser.Parse(fs, new TechFamily());
                }
                using (FileStream fs = new FileStream(societyPath, FileMode.Open))
                {
                    societyTech = ParadoxParser.Parse(fs, new TechFamily());
                }
                LoadSelectedList();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void btnOpenFold_Click(object sender, RoutedEventArgs e)
        {
            fold = MainWindow.Instance.GetFold();
            System.Diagnostics.Process.Start("explorer.exe", fold + @"\common\technology\technologies\");
        }
    }
}
